home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / MyDeviceLoop / MyDeviceLoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  7.3 KB  |  283 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        MyDeviceLoop.c
  3.  
  4.     Contains:    This snippet shows how to write a device loop that            
  5.                 works under System 7 and pre-7.0 systems.  As                
  6.                 described on pages 21-23 and 21-24 of Inside Mac            
  7.                 volume VI, a device loop procedure searches all                
  8.                 active screen devices, calling a drawing procedure            
  9.                 whenever it encounters a screen that intersects            
  10.                 the drawing region.  In this app the drawing                
  11.                 region is the app's window bounds and the chosen            
  12.                 drawing procedure simply displays the screen's                
  13.                 colors for every device the window bounds intersect.        
  14.  
  15.     Written by: EL    
  16.  
  17.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  18.  
  19.                 You may incorporate this Apple sample source code into your program(s) without
  20.                 restriction. This Apple sample source code has been provided "AS IS" and the
  21.                 responsibility for its operation is yours. You are not permitted to redistribute
  22.                 this Apple sample source code as "Apple sample source code" after having made
  23.                 changes. If you're going to re-distribute the source, we require that you make
  24.                 it clear in the source that the code was descended from Apple sample source
  25.                 code, but that you've made changes.
  26.  
  27.     Change History (most recent first):
  28.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  29.                                             for demonstration purposes.
  30.                 7/12/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  31.                 
  32.  
  33. */
  34. #include "CarbonPrefix.h"
  35. #include <Dialogs.h>
  36. #include <Fonts.h>
  37.  
  38. /* Constant Declarations */
  39.  
  40. #define    WWIDTH        400
  41. #define    WHEIGHT        256
  42.  
  43. //#define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  44. //#define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  45.  
  46.  
  47. enum PointSelector {topLeft,
  48.             botRight};
  49. /* Global Variable Definitions */
  50.  
  51. WindowPtr    gWindow;
  52.  
  53. void initMac();
  54. void createWindow();
  55. void doMyDeviceLoop();
  56. void doDraw();
  57. void doEventLoop();
  58.  
  59. void main(void)
  60. {
  61.     initMac();
  62.     
  63.     createWindow();
  64.  
  65.     doEventLoop();
  66. }
  67.  
  68. void initMac()
  69. {
  70.     /*MaxApplZone();
  71.  
  72.     InitGraf( &qd.thePort );
  73.     InitFonts();
  74.     InitWindows();
  75.     InitMenus();
  76.     TEInit();
  77.     InitDialogs( nil );*/
  78.     InitCursor();
  79.     FlushEvents( 0, everyEvent );
  80. }
  81.  
  82. void createWindow()
  83. {
  84.     Rect rect;
  85.     
  86.     BitMap    bitMap;
  87.     int    top, left;
  88.     
  89.     GetQDGlobalsScreenBits(&bitMap);
  90.     
  91.     top = (((bitMap.bounds.bottom - bitMap.bounds.top) - WHEIGHT) / 2);
  92.     left = (((bitMap.bounds.right - bitMap.bounds.left) - WWIDTH) / 2);
  93.     
  94.     //SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  95.     SetRect( &rect, left, top, left + WWIDTH, top + WHEIGHT );
  96.     
  97.     gWindow = NewCWindow( 0L, &rect, "\pMyDeviceLoop", true, documentProc,
  98.                             (WindowPtr)-1L, true, 0L );                        
  99.     //SetPort( gWindow );
  100.     SetPortWindowPort( gWindow );
  101.     
  102.     TextFont( kFontIDTimes );
  103.     TextSize( 48 );
  104.     TextMode( srcCopy );
  105. }
  106.  
  107.  
  108. // Old version of doMyDeviceLoop, new version capable of supporting multiple monitors
  109. /*
  110. void doMyDeviceLoop()
  111. {
  112.     int            depth;
  113.     Rect        gDeviceRect;
  114.     Rect        intersectingRect;
  115.     GDHandle    gDevice;
  116.     //Point        point;
  117.     Rect        tempRect1;
  118.     
  119.     // Get the handle to the first device in the list. 
  120.     gDevice = GetDeviceList();
  121.     
  122.     // Loop through all the devices in the list. 
  123.     while (gDevice != nil)
  124.     {
  125.         // Get the device's gdRect and convert it to local coordinates. 
  126.         gDeviceRect = (**gDevice).gdRect;
  127.         depth = (**(**gDevice).gdPMap).pixelSize;
  128.             
  129.         GlobalToLocal( topLeft );
  130.         GlobalToLocal( (Point *)botRight );
  131.         
  132.         // Check if the app's window rect intersects the device's, and if it 
  133.         //    does, set the clip region's rect to the intersection, then DRAW! 
  134.         
  135.         //if (SectRect( &gWindow->portRect, &gDeviceRect, &intersectingRect ))
  136.         if (SectRect( GetPortBounds(GetWindowPort(gWindow), &tempRect1), &gDeviceRect, &intersectingRect ))
  137.         {
  138.             ClipRect( &intersectingRect );
  139.             doDraw( depth, &intersectingRect );
  140.         }
  141.         
  142.         // Get the next device in the list. 
  143.         gDevice = GetNextDevice( gDevice );
  144.     }
  145. }
  146. */
  147.  
  148. // New doMyDeviceLoop, now works with multiple monitors
  149. void doMyDeviceLoop()
  150. {
  151.     int                depth;
  152.     Rect            gDeviceRect;
  153.     Rect            intersectingRect;
  154.     GDHandle        gDevice;
  155.     //WindowRecord    *windowRec = (WindowRecord *)gWindow;
  156.     WindowPtr        windowRec = gWindow;
  157.     Rect            windowRect; //= (**windowRec->contRgn).rgnBBox;
  158.     RgnHandle        rgnHandle = NewRgn();
  159.     
  160.     GetWindowRegion(windowRec, kWindowContentRgn, rgnHandle);
  161.     GetRegionBounds(rgnHandle, &windowRect);
  162.  
  163.     // Get the handle to the first device in the list. 
  164.     gDevice = GetDeviceList();
  165.  
  166.     // Loop through all the devices in the list. 
  167.     while (gDevice != nil)
  168.     {
  169.         // Get the device's gdRect  */
  170.         gDeviceRect = (**gDevice).gdRect;
  171.         depth = (**(**gDevice).gdPMap).pixelSize;
  172.  
  173.         // Check if the app's window rect intersects the device's, and if it 
  174.         // does, set the clip region's rect to the intersection, then DRAW! 
  175.         if (SectRect( &windowRect, &gDeviceRect, &intersectingRect ))
  176.         {
  177.             // The intersectingRect is in global coords. Convert to local 
  178.             GlobalToLocal((Point *)&intersectingRect.top);
  179.             GlobalToLocal((Point *)&intersectingRect.bottom);
  180.  
  181.             ClipRect( &intersectingRect );
  182.             doDraw( depth, &intersectingRect );
  183.         }
  184.  
  185.         // Get the next device in the list. 
  186.         gDevice = GetNextDevice( gDevice );
  187.     }
  188.     
  189.     DisposeRgn(rgnHandle);
  190. }
  191.  
  192.  
  193. void doDraw( depth)
  194. int        depth;
  195. {
  196.     int                i;
  197.     int                totalColors;
  198.     int                penThickness;
  199.     //RGBColor        color;
  200.     CTabHandle        ctable;
  201.     Str255            string = "\pDirect Colors";
  202.     Rect            tempRect1;
  203.     
  204.     if (depth > 8)
  205.     {
  206.         BackColor( blackColor );
  207.         ForeColor( blueColor );
  208.         
  209.         /* Draw text for direct colors mode. */
  210.         //EraseRect( &gWindow->portRect );
  211.         EraseRect( GetPortBounds(GetWindowPort(gWindow), &tempRect1));
  212.         //MoveTo( (gWindow->portRect.right - StringWidth( string )) / 2, 130 );
  213.         MoveTo((GetPortBounds(GetWindowPort(gWindow), &tempRect1)->right - StringWidth(string)) / 2, 130);
  214.         DrawString( string );
  215.     }
  216.     else
  217.     {
  218.         /* Get the colortable at this depth. */
  219.         ctable = GetCTable( depth );
  220.         
  221.         /* Set the line thickness to a fraction of the window height. */
  222.         totalColors = (2 << depth) / 2;
  223.         //penThickness = gWindow->portRect.bottom / totalColors;
  224.         penThickness = GetPortBounds(GetWindowPort(gWindow), &tempRect1)->bottom / totalColors;
  225.         PenSize( 1, penThickness );
  226.     
  227.         /* Now draw the colors at this depth. */
  228.         for (i = 0; i < totalColors; i++)
  229.         {
  230.             RGBForeColor( &(**ctable).ctTable[i].rgb );
  231.             MoveTo( 0, i * penThickness );
  232.             //LineTo( gWindow->portRect.right, i * penThickness );
  233.             LineTo(GetPortBounds(GetWindowPort(gWindow), &tempRect1)->right, i * penThickness);
  234.         }
  235.         
  236.         /* Release the colortable memory. */
  237.         DisposeCTable( ctable );
  238.     }
  239. }
  240.  
  241. void doEventLoop()
  242. {
  243.     EventRecord event;
  244.     WindowPtr   window;
  245.     short       clickArea;
  246.     Rect        screenRect;
  247.  
  248.     for (;;)
  249.     {
  250.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  251.         {
  252.             if (event.what == mouseDown)
  253.             {
  254.                 clickArea = FindWindow( event.where, &window );
  255.                 
  256.                 if (clickArea == inDrag)
  257.                 {
  258.                     //screenRect = (**GetGrayRgn()).rgnBBox;
  259.                     GetRegionBounds(GetGrayRgn(), &screenRect);
  260.                     DragWindow( window, event.where, &screenRect );
  261.                 }
  262.                 else if (clickArea == inContent)
  263.                 {
  264.                     if (window != FrontWindow())
  265.                         SelectWindow( window );
  266.                 }
  267.                 else if (clickArea == inGoAway)
  268.                     if (TrackGoAway( window, event.where ))
  269.                         return;
  270.             }
  271.             else if (event.what == updateEvt)
  272.             {
  273.                 window = (WindowPtr)event.message;    
  274.                 //SetPort( window );
  275.                 SetPortWindowPort( window );
  276.                 
  277.                 BeginUpdate( window );
  278.                 doMyDeviceLoop();
  279.                 EndUpdate( window );
  280.             }
  281.         }
  282.     }
  283. }